package com.lin.carbon.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.carbon.common.Result;
import com.lin.carbon.entity.SysDict;
import com.lin.carbon.service.SysDictService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/SysDict")
@CrossOrigin
public class SysDictController  {

    @Autowired
    SysDictService sysDictService;

    //查询所有(实际是分页查询)
    @GetMapping("/getPageList")
    public Result<IPage<SysDict>> getSysDictPageList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10")Integer pageSize)
    {
        Page<SysDict> page = new Page<>(pageNum, pageSize);
        IPage<SysDict> resultPage = sysDictService.page(page, null);
        return Result.success(resultPage);
    }

    //按照ID查询
    @GetMapping("/getById")
    public Result<SysDict> get(long id){
        SysDict sysDict = sysDictService.getById(id);
        return Result.success(sysDict);
    }

    //按照字典名称模糊查询
    @PostMapping("/getByName")
    public Result<IPage<SysDict>> getByName(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10")Integer pageSize,
            @RequestParam(defaultValue = "") String dictName
    ){
        Page<SysDict> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SysDict> wrapper = new QueryWrapper<>();
        wrapper.like("dict_name", dictName);
        IPage<SysDict> resultPage = sysDictService.page(page, wrapper);
        return Result.success(resultPage);
    }

    /**
     * 添加字典主表记录
     * @param sysDict 字典实体对象
     * @return 统一响应结果
     */
    @PostMapping("/add")
    public Result<Boolean> addSysDict(@Valid @RequestBody SysDict sysDict) {
        boolean result = sysDictService.addSysDict(sysDict);
        return Result.success(result);
    }

    // 查询所有
    @GetMapping("/getAll")
    public Result<List<SysDict>> getAll() {
        // 调用 service 层的 list 方法获取所有字典记录
        List<SysDict> allSysDicts = sysDictService.list();
        return Result.success(allSysDicts);
    }
    /**
     * 修改系统字典
     * @param sysDict 包含修改信息的字典实体对象
     * @return 统一响应结果，操作成功返回 true，失败返回 false
     */
    @PutMapping("/update")
    public Result<Boolean> updateSysDict(@Valid @RequestBody SysDict sysDict) {
        boolean result = sysDictService.updateSysDict(sysDict);
        return Result.success(result);
    }

    /**
     * 删除系统字典
     * @param id 要删除的系统字典的 ID
     * @return 统一响应结果，操作成功返回 true，失败返回 false
     */
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteSysDict(@PathVariable Long id) {
        boolean result = sysDictService.deleteSysDict(id);
        return Result.success(result);
    }







}

